package com.xinnet.mms.web;

import java.util.List;
import java.util.concurrent.TimeoutException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.rubyeye.xmemcached.exception.MemcachedException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.product.service.ProductService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/ruleSet/approval/intentionCustomBrokerageApproval")
public class IntentionCustomBrokerageApprovalController {
	
	@Autowired
	ProductService productService;
	@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
	@RequestMapping(value = "/intentionCustomBrokerageApprovalPage")
	public String intentionCustomBrokerageApprovalPage(HttpServletRequest request,HttpServletResponse response){
		List<Product> productList = productService.queryEnableProduct();
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(productList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(productList.size(), productList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,productList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),productList);
		}
		request.setAttribute("pageView", pageView);
		return "/ruleSet/approval/intentionCustomBrokerageApproval";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/approval")
	public String approval(HttpServletRequest request,HttpServletResponse response) throws TimeoutException, InterruptedException, MemcachedException{
		String productId = request.getParameter("productId");
		String isEffectCustomDivided = request.getParameter("isEffectCustomDivided");
		Product product = new Product();
		product.setId(productId);
		product.setIsEffectCustomDivided(isEffectCustomDivided);
		productService.updateProduct(product);
		return "redirect:/ruleSet/approval/intentionCustomBrokerageApproval/intentionCustomBrokerageApprovalPage";
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathApproval")
	public String bathApproval(HttpServletRequest request,HttpServletResponse response) throws TimeoutException, InterruptedException, MemcachedException{
		String isEffectCustomDivided = request.getParameter("isEffectCustomDivided");
		String productIdStr = request.getParameter("productIdStr");
		String[] productIdArray = productIdStr.split(",");
		for (int i = 0; i < productIdArray.length; i++) {
			String productId = productIdArray[i];
			Product product = new Product();
			product.setId(productId);
			product.setIsEffectCustomDivided(isEffectCustomDivided);
			productService.updateProduct(product);
		}
		return "redirect:/ruleSet/approval/intentionCustomBrokerageApproval/intentionCustomBrokerageApprovalPage";
	}

}
